package com.xgst.land.dialog

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.widget.TextView
import com.blankj.utilcode.util.AdaptScreenUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.xgst.land.R
import com.xgst.land.activity.check.InventoryScanLandActivity

class ReplaceDialog(context: Context) : Dialog(context, R.style.dialogStyle) {

    private lateinit var mClick:(String,Int) -> Unit
    private var mPosition = -1;
    private val tvUID:TextView
    private var tvReplaceScan:TextView
    private var mActivity:InventoryScanLandActivity

    init {
        mActivity = context as InventoryScanLandActivity
        val view =
            LayoutInflater.from(context).inflate(R.layout.dialog_replace_layout, null, false)
        val tvSubmit = view.findViewById<TextView>(R.id.tvSubmit);
        tvReplaceScan = view.findViewById(R.id.tvReplaceScan);
        tvUID = view.findViewById(R.id.tvUID);
        setContentView(view)
        val attributes = window!!.attributes
        attributes.width= AdaptScreenUtils.px2Pt(600f)
        attributes.height= AdaptScreenUtils.px2Pt(400f)
        window!!.attributes = attributes
        setCancelable(true)
        tvSubmit.setOnClickListener {
            if(tvUID.text == null){
                ToastUtils.showShort("没有UID数据")
                return@setOnClickListener
            }
            mClick(tvUID.text.toString(),mPosition)
        }
        tvReplaceScan.setOnClickListener {
            mActivity.startScan(true)
            tvReplaceScan.text = "开始扫描..."
        }
    }

    fun showDialog(position:Int,onClick:(String,Int)->Unit){
        mPosition = position
        mClick = onClick
        show()
    }

    fun setUID(uid:String?){
        tvUID.text = uid
        tvReplaceScan.text = "开始扫描"
    }
}